package com.powernobug.service.impl;

import com.github.pagehelper.PageHelper;
import com.powernobug.bean.model.MarketStorage;
import com.powernobug.bean.model.MarketStorageExample;
import com.powernobug.bean.vo.PageVo;
import com.powernobug.bean.vo.param.StorageListParam;
import com.powernobug.mapper.MarketStorageMapper;
import com.powernobug.service.StorageService;
import com.powernobug.util.MyBatisUtil;
import com.powernobug.util.VerifyUtil;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author: 肖云戈
 * @CreateTime: 2024-08-22
 * @Description:
 */
public class StorageServiceImpl implements StorageService {
    private static SqlSession tempSession;

    @Override
    public MarketStorage uploadFile(MarketStorage marketStorage) {
        tempSession = MyBatisUtil.getSqlSession();
        MarketStorageMapper storageMapper = tempSession.getMapper(MarketStorageMapper.class);
        storageMapper.insert(marketStorage);
        System.out.println(marketStorage);

        MyBatisUtil.releaseSqlSession(tempSession);
        return marketStorage;
    }

    @Override
    public MarketStorage fetchPicture(String imgName) {
        tempSession = MyBatisUtil.getSqlSession();
        MarketStorageMapper storageMapper = tempSession.getMapper(MarketStorageMapper.class);
        return storageMapper.selectByKey(imgName);
    }

    @Override
    public PageVo getStorageByPage(StorageListParam params) {
        MarketStorageExample example = new MarketStorageExample();
        if (params.getPage() == null || params.getLimit() == null) {
            throw new PersistenceException();
        }
        if (!(params.getSort() == null) && !(params.getOrder() == null)) {
            example.setOrderByClause(params.getSort() + " " + params.getOrder());
        }
        MarketStorageExample.Criteria criteria = example.createCriteria();
        if (params.getName() != null) {
            criteria.andNameEqualTo(params.getName());
        }
        if (params.getKey() != null) {
            criteria.andKeyEqualTo(params.getKey());
        }
        criteria.andDeletedEqualTo(false);

        tempSession = MyBatisUtil.getSqlSession();
        MarketStorageMapper storageMapper = tempSession.getMapper(MarketStorageMapper.class);
        PageHelper.startPage(params.getPage(), params.getLimit());
        List<MarketStorage> targetStorages = storageMapper.selectByExample(example);
        MyBatisUtil.releaseSqlSession(tempSession);
        return PageVo.pag(targetStorages);
    }

    @Override
    public Boolean updateStorage(MarketStorage marketStorage, boolean deleteMode) throws NoSuchFieldException, IllegalAccessException {
        // 先校验一下 待修改的数据是否在数据库中、只有name能被修改
        tempSession = MyBatisUtil.getSqlSession();
        MarketStorageMapper storageMapper = tempSession.getMapper(MarketStorageMapper.class);
        MarketStorage verify = storageMapper.selectByPrimaryKey(marketStorage.getId());
        if (verify == null) {
            return false;
        } else if (!VerifyUtil.storageInfoVerify(marketStorage, verify, deleteMode)) {
            return false;
        }

        // 校验成功后，直接修改即可
        storageMapper.updateByPrimaryKey(marketStorage);
        MyBatisUtil.releaseSqlSession(tempSession);
        return true;
    }
}
